<?php

if(get('reset')==1){

    if(Antz_IntelliForm::submitted('confirm-reset')){
        $pData[1] = array();
        $pData[2] = array();
        $pData[3] = array();
        $pData[4] = array();
        unset($_SESSION['JFX_Module_Vms_ScanFilesTemp'], $_SESSION['JFX_Module_Vms_ScanFilesAuto']);
        JFX::redirect($this->adminUrl.'/new-version/');
    }


    $formFields = array(
        array(
            'type' => 'submit',
            'name' => 'submitBtn',
            'label' => '&nbsp;',
            'value' => 'Reset Package Details'
        )
    );

    echo JFX::makeRapidForm($formFields, 'Confirm Reset', 'confirm-reset');

    return;
}




































if(Antz_IntelliForm::submitted('new-version')){
    $error = false;
    $title = post('title');
    $keyname = post('keyname');
    $version = post('version');
    $basePath = post('basepath');
    $changelog = post('change_log');
    $packageid = (int) post('package');
    $USER = JFX::registry('JFX_User');

    if($packageid == 0){
        // creating a new package
        $dbParams = array(
            'keyname' => $keyname,
            'base_path' => $basePath,
            'created_tstamp' => time(),
            'owner_id' => $USER->userid,
            'current_version' => '0.0.0'
        );
        $packageid = $DB->insert($CONFIG->dbprefix.'vms_packages', $dbParams);
        $this->updateContentLang('package_title', $packageid, $title);
    }else{
        if($DB->countRows($CONFIG->dbprefix.'vms_packages', "id = '{$packageid}'")==0){
            JFX::addError('Invalid Package ID');
            $error = true;
        }else{
            $title = $this->getContentLang('package_title', $packageid);
            $basePath = $DB->oneValue($CONFIG->dbprefix.'vms_packages', 'base_path', "id = '{$packageid}'");
            $keyname = $DB->oneValue($CONFIG->dbprefix.'vms_packages', 'keyname', "id = '{$packageid}'");
        }
    }

    if($DB->countRows($CONFIG->dbprefix.'vms_packages', "id = '{$packageid}'")==0){
        $error = true;
        JFX::addError('Invalid package id');
    }

    if(trim($changelog) == ''){
        $error = true;
        JFX::addError('Changelog must contain some description');
    }

    if(!$VM->isLaterVersion($version, $VM->getCurrentVersion($packageid))){
        $error = true;
        JFX::addError('Version must be greater than '.$VM->getCurrentVersion($packageid));
    }

    eval('$basePath = "'.$basePath.'";');
    if(!is_dir($basePath)){
        $error = true;
        JFX::addError('Base Path is not a directory');
    }

    if(!$error){
        // ok to process
        $newParams = array(
            'package_id' => $packageid,
            'version' => $version,
            'base_path' => $DB->oneValue($CONFIG->dbprefix.'vms_packages', 'base_path', "id = '{$packageid}'"),
            'title' => $title,
            'change_log' => $changelog
        );
        $pData[1] = $newParams;
        JFX::redirect(THIS_PAGE_URL);
    }
}





$packages = $DB->fetchAll("SELECT * FROM {$CONFIG->dbprefix}vms_packages ORDER BY keyname ASC");
if(!is_array($packages)) $packages = array();
$pOpts = array(0 => 'New Package ...');
foreach($packages as $k=>$v){
    $pOpts[$v['id']] = $this->getContentLang('package_title', $v['id']);
}

$btn = '<input type="button" id="vms-autoVersionBtn" value="Auto Version" />';

if(post('package')>0 || count($pData[1])>0){
    $hideClass = 'hide';
}else{
    $hideClass = '';
}

$VIEW->addCss('.hide{display: none;}');

$formFields = array(
    array(
        'type' => 'select',
        'id' => 'vms-package',
        'label' => 'Package',
        'name' => 'package',
        'options' => $pOpts,
        'value' => post('package', $pData[1]['package_id'])
    ),
    array(
        'type' => 'text',
        'id' => 'vms-title',
        'name' => 'title',
        'label' => 'Title',
        'value' => post('title'),
        'class' => $hideClass
    ),
    array(
        'type' => 'text',
        'id' => 'vms-keyname',
        'name' => 'keyname',
        'label' => 'Keyname',
        'value' => post('keyname'),
        'class' => $hideClass
    ),
    array(
        'type' => 'text',
        'id' => 'vms-basepath',
        'name' => 'basepath',
        'value' => post('basepath', '{$CONFIG->modulesDir}/keyname'),
        'label' => 'Base path',
        'class' => 'wide '.$hideClass
    ),
    array(
        'type' => 'text',
        'id' => 'vms-version',
        'name' => 'version',
        'label' => 'Version',
        'value' => post('version', $pData[1]['version']),
        'aftertext' => $btn
    ),
    array(
        'type' => 'be_tinymce',
        'id' => 'changelog',
        'name' => 'change_log',
        'label' => 'Change Log',
        'value' => post('change_log', $pData[1]['change_log'])
    ),
    array(
        'type' => 'submit',
        'value' => 'Save',
        'label' => '&nbsp;',
        'name' => 'submitBtn'
    )
);

// add ajax auto version button
$js = <<<JS

function VMS_AutoVersionInsert(json){
    $('#vms-version').get(0).value = json.version;
}

$(document).ready(function(){
    $('#vms-autoVersionBtn').click(function(){
        var sel = $('#vms-package').get(0);
        var params = new Object;
        params.id = sel.options[sel.selectedIndex].value;
        if(params.id*1 === 0){
            $('#vms-version').get(0).value = '0.0.1';
        }else{
            $.getJSON('{$this->adminUrl}/ajax/get-next-version', params, VMS_AutoVersionInsert);
        }
    });
    $('#vms-package').change(function(){
        var id = this.options[this.selectedIndex].value;
        if(id*1 === 0){
            // show the title and keyname for new package
            $('#vms-title_component, #vms-keyname_component, #vms-basepath_component').css('display', 'block');
            $('#vms-version').get(0).value = '0.0.1';
        }else{
            // hide the title and keyname
            $('#vms-title_component, #vms-keyname_component, #vms-basepath_component').css('display', 'none');
            params = {'id' : id};
            $.getJSON('{$this->adminUrl}/ajax/get-next-version', params, VMS_AutoVersionInsert);
        }
    });
});
JS;


$VIEW->addJs($js);






?>
<div align="left">
<p>Select the package you want to update, enter the version number and note any changes.
After this you will select the files that apply to this version.</p>
</div>

<?php

echo JFX::makeRapidForm($formFields, 'New Version Details', 'new-version');








